package org.jmc.refmanager.web.wicket.form;

import java.util.Date;
import java.util.Set;

import org.apache.wicket.MarkupContainer;
import org.apache.wicket.PageParameters;
import org.apache.wicket.datetime.markup.html.basic.DateLabel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.jmc.refmanager.domain.model.Change;
import org.jmc.refmanager.domain.model.OrgUnit;
import org.jmc.refmanager.domain.model.OrgUnit.Complement;
import org.jmc.refmanager.util.ListUtil;
import org.jmc.refmanager.util.Util;
import org.jmc.refmanager.web.wicket.model.EnumPropertyModel;
import org.jmc.refmanager.web.wicket.model.ServiceModel;
import org.jmc.refmanager.web.wicket.page.navigation.NavigationPage;
import org.jmc.refmanager.web.wicket.page.service.ServiceDeletePage;
import org.jmc.refmanager.web.wicket.page.service.ServiceEditPage;
import org.jmc.refmanager.web.wicket.page.service.ServiceNewPage;
import org.jmc.refmanager.web.wicket.page.service.ServiceViewPage;

public class ServiceViewForm extends Form<OrgUnit> {
	private static final long serialVersionUID = 1L;

	public ServiceViewForm(String id, ServiceModel model, final Class<? extends WebPage> pageOrigin) {
		super(id, model);
		setModel(new CompoundPropertyModel<OrgUnit>(model));
		
		final boolean isTreeBrowsing = pageOrigin.getName().equals(NavigationPage.class.getName());

		OrgUnit viewService = model.getObject();
		final Integer serviceId = viewService.getId();

		// Ce service est en cours de modification depuis le 01/12/2010.
		Set<Change> changes = viewService.getChanges();
		Change change = ListUtil.getFirstEvenIfNull(changes);
		
		if (change != null) {
			this.warn(String.format("Ce service est en cours de modification depuis le %1$td/%1$tm/%1$tY.",
					change.getCreationDate()));
		}
		
		add(new Label("codeUO", new PropertyModel<String>(viewService, "codeUO")));
		String saaOrSao;
		if (viewService.isSaa()) {
			saaOrSao = "Service administratif";
		} else if (viewService.isSao()) {
			saaOrSao = "Service opérationnel";
		} else {
			saaOrSao = "n/a";
		}
		add(new Label("type", new PropertyModel<String>(viewService, "type")));
		add(new Label("parentType", new PropertyModel<String>(viewService, "parent.type")));
		add(new Label("saaOrSao", saaOrSao));
		add(new Label("libCourt", new PropertyModel<String>(viewService, "libCourt")));
		add(new Label("libLong", new PropertyModel<String>(viewService, "libLong")));
		add(new Label("libComplet", new PropertyModel<String>(viewService, "libComplet")));

		add(DateLabel.forDatePattern("dateEffet1", 
				new PropertyModel<Date>(viewService, "dateEffet1"), "dd/MM/yyyy"));
		
		add(new Label("adressePostale", new PropertyModel<String>(viewService, "adressePostale")));
		
		boolean isParentDirectionvalidation = false;
		if (viewService.getParent() != null) {
			// Bloquer la navigation au-dessus de la direction de validation
			String parentType = Util.getBlankIfNull(viewService.getParent().getType());
			isParentDirectionvalidation = parentType.equals("C02");
		}
		
		MarkupContainer viewParentLink = new Link<Integer>("viewParentLink", new PropertyModel<Integer>(viewService, "parent.id")) {
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick() {
				PageParameters params = new PageParameters();
				params.add("id", getDefaultModelObjectAsString());
				
				if (isTreeBrowsing) {
					setResponsePage(pageOrigin, params);
				} else {
					setResponsePage(ServiceViewPage.class, params);
				}
				
			}
		}.add(new Label("parentLibCourt", new PropertyModel<String>(viewService, "parent.libCourt")));
		viewParentLink.setEnabled(!isParentDirectionvalidation);
		add(viewParentLink);

		add(new Label("numRue", new PropertyModel<String>(viewService, "numRue")));
		add(new Label("complementNumRue", 
				new EnumPropertyModel<Complement>(viewService, "complementNumRue", this, Complement.class)));

		add(new Label("adresse", new PropertyModel<String>(viewService, "adresse")));
		add(new Label("complementAdresse", new PropertyModel<String>(viewService, "complementAdresse")));
		add(new Label("codePostal", new PropertyModel<String>(viewService, "codePostal")));
		add(new Label("commune", new PropertyModel<String>(viewService, "commune")));
		add(new Label("mail", new PropertyModel<String>(viewService, "mail")));
		add(new Label("telephone", new PropertyModel<String>(viewService, "telephone")));
		add(new Label("rescom", new PropertyModel<String>(viewService, "rescom")));

		// -- ACTIONS
		add(new Button("btnCreate") {
			private static final long serialVersionUID = 1L;

			/* (non-Javadoc)
			 * @see org.apache.wicket.markup.html.form.Button#onSubmit()
			 */
			@Override
			public void onSubmit() {
				PageParameters params = new PageParameters();
				params.add("parentId", String.valueOf(serviceId));
				setResponsePage(ServiceNewPage.class, params);
			}
		});

		boolean serviceChangeInProgress = (viewService.getChanges() != null && viewService.getChanges().size() != 0);
		boolean isDirectionValidation = Util.getBlankIfNull(viewService.getType()).equalsIgnoreCase("C02");
		boolean isRoot = "SCMI0000".equalsIgnoreCase(viewService.getCodeUO());
		
		add(new Button("btnEdit") {
			private static final long serialVersionUID = 1L;

			/* (non-Javadoc)
			 * @see org.apache.wicket.markup.html.form.Button#onSubmit()
			 */
			@Override
			public void onSubmit() {
				PageParameters params = new PageParameters();
				params.add("id", String.valueOf(serviceId));
				setResponsePage(ServiceEditPage.class, params);
			}
		}.setVisible(!(serviceChangeInProgress || isRoot)));
		
		add(new Button("btnDelete") {
			private static final long serialVersionUID = 1L;

			/* (non-Javadoc)
			 * @see org.apache.wicket.markup.html.form.Button#onSubmit()
			 */
			@Override
			public void onSubmit() {
				PageParameters params = new PageParameters();
				params.add("id", String.valueOf(serviceId));
				setResponsePage(ServiceDeletePage.class, params);
			}
		}.setVisible(!(serviceChangeInProgress || isDirectionValidation || isRoot)));

		Button btnCancel = new Button("btnCancel") {
			private static final long serialVersionUID = 1L;
			@Override
			public void onSubmit() {
				setResponsePage(pageOrigin);
			}
		};
		// Pas de validation
		btnCancel.setDefaultFormProcessing(false);
		btnCancel.setVisible(!isTreeBrowsing);
		add(btnCancel);
	}
}
